<?php
namespace app\mobile\model;

use think\Model;
class Policy extends Model
{
	public function getPolicyList($user_id,$role,$keyword,$pageSize)
	{
		if ($role == 'admin'){
			if ($keyword == false || $keyword == ''){
				$policy_list = db('policy')->field(['id','policy','created','createdwho'])->where(['if_publish'=>'1'])->paginate($pageSize);
			}else {
				$policy_list = db('policy')->field(['id','policy','created','createdwho'])->where(['if_publish'=>'1'])->where('policy','like','%'.$keyword.'%')->paginate($pageSize);
			}
		}else {
			$user_warehouse = db('ouser')->where('id',$user_id)->value('warehouse_id');
			if ($keyword == false || $keyword == ''){
				$policy_list = db('policy')->field(['id','policy','created','createdwho'])->where(['if_publish'=>'1'])->where('to_where','like','%"'.$user_warehouse.'"%')->paginate($pageSize);
			}else {
				$policy_list = db('policy')->field(['id','policy','created','createdwho'])->where(['if_publish'=>'1'])->where('policy','like','%'.$keyword.'%')->where('to_where','like','%"'.$user_warehouse.'"%')->paginate($pageSize);	
			}			
		}
		
		$policy_list = $policy_list->toArray()['data'];
		if (is_array($policy_list)){
			foreach ($policy_list as $pk => &$pv){
				$pv['createdwho'] = db('user')->where('id',$pv['createdwho'])->value('user_name');
				$pv['created'] = date('Y-m-d',$pv['created']);
			}
		}
		
		
		return $policy_list;
	}
}